﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Cleverscape.UTorrentClient.WebClient.ServiceDefinition;
using System.ComponentModel;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Threading;

namespace Cleverscape.UTorrentClient.WebClient
{
    /// <summary>
    /// Represents a collection of Feeds
    /// </summary>
    public class FeedItemCollection : IList<FeedItem>, INotifyCollectionChanged, INotifyPropertyChanged
    {

        private List<FeedItem> _FeedItemCollectionInternal;

        internal UTorrentWebClient ParentClient { get; private set; }

        internal FeedItemCollection(UTorrentWebClient ParentClient)
        {
            this.ParentClient = ParentClient;
            _FeedItemCollectionInternal = new List<FeedItem>();
        }

        #region Internal Methods

        
        private void CallCollectionChanged(NotifyCollectionChangedEventArgs EventArgs)
        {
            DispatcherHelper.UIDispatcher.BeginInvoke(() =>
            {
                if (CollectionChanged != null)
                {
                    if (ParentClient.Dispatcher == null || ParentClient.Dispatcher.CheckAccess())
                    {
                        CollectionChanged(this, EventArgs);
                    }
                    else
                    {
                        ParentClient.Dispatcher.BeginInvoke(new CallCollectionChangedAsyncCallback(CallCollectionChangedAsync), new object[] { EventArgs });
                    }
                }
            });
        }

        private void CallCollectionChangedAsync(NotifyCollectionChangedEventArgs EventArgs)
        {
            CollectionChanged(this, EventArgs);
        }

        private delegate void CallCollectionChangedAsyncCallback(NotifyCollectionChangedEventArgs EventArgs);

        internal void AddFeed(FeedItem FeedItemToAdd)
        {
            _FeedItemCollectionInternal.Add(FeedItemToAdd);
            CallCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, FeedItemToAdd, _FeedItemCollectionInternal.Count-1));
        }

        #endregion

        #region Private Methods





        #endregion

        #region Public Methods

        public bool Contains(string Name)
        {
            foreach (FeedItem CurrentFeedItem in _FeedItemCollectionInternal)
            {
                if (CurrentFeedItem.Name == Name)
                {
                    return true;
                }
            }
            return false;
        }

        public FeedItem GetByName(string Name)
        {
            foreach (FeedItem CurrentFeedItem in _FeedItemCollectionInternal)
            {
                if (CurrentFeedItem.Name == Name)
                {
                    return CurrentFeedItem;
                }
            }
            return null;
        }



        public void Insert(int index, FeedItem item)
        {
        }

        public void Clear()
        {
        }

        public void Add(FeedItem item)
        {
            AddFeed(item);
        }

        public int IndexOf(FeedItem item)
        {
            return _FeedItemCollectionInternal.IndexOf(item);
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        public FeedItem this[int index]
        {
            get
            {
                return _FeedItemCollectionInternal[index];
            }
            set
            {
            }
        }

        public bool Contains(FeedItem item)
        {
            return (GetByName(item.Name) != null);
        }

        public void CopyTo(FeedItem[] array, int arrayIndex)
        {
            _FeedItemCollectionInternal.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _FeedItemCollectionInternal.Count; }
        }

        public bool IsReadOnly
        {
            get { return true; }
        }

        public bool Remove(FeedItem item)
        {
            if (GetByName(item.Name) != null)
            {
                int idx = _FeedItemCollectionInternal.IndexOf(item);
                _FeedItemCollectionInternal.Remove(item);
                CallCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, idx));
                return true;
            }
            return false;
        }

        public IEnumerator<FeedItem> GetEnumerator()
        {
            return _FeedItemCollectionInternal.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _FeedItemCollectionInternal.GetEnumerator();
        }

        #endregion

        #region Public Events (Interface Implementation)

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

        #endregion

    }
}
